﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data.SqlClient;
using MaxLeaf.DB;
using System.Data;


namespace MaxLeaf.DAL.K3
{
    /// <summary>
    /// 部门
    /// </summary>
    public class t_departmentDAL : BaseDAL
    {
        public t_departmentDAL()
            : base()
        {
            strTableName = "t_Department";
            strFieldNames = "FManager,FDeleted,FPhone,FFax,FDProperty,FIsCreditMgr,FAcctID,FNote,FCostAccountType,FOtherARAcctID,FPreARAcctID,FOtherAPAcctID,FPreAPAcctID,FShortNumber,FNumber,FName,FParentID";
        }
        protected override void Update_Before(DBAccess dbOrgCon, DBAccess dbCon, DataRow drOrgData, DataRow drData)
        {
            string strSql = string.Empty;
            KeyValuePair<string, IDataParameter[]> kvp = new KeyValuePair<string, IDataParameter[]>();

            strSql = "Update t_ICItem Set FParentID =  @FParentID  Where  FItemID=@FItemID";
            kvp = dbCon.CreateCondParam(new Dictionary<string, object>() { { "FParentID", drData["FParentID"] }, { "FItemID", drData["FItemID"] } });
            dbCon.ExecuteSql(strSql, kvp.Value);

            strSql = "Update t_Item Set FParentID =  @FParentID ,FDeleted=@FDeleted,Fnumber=@Fnumber,FName=@FName,FShortNumber=@FShortNumber,FFullNumber=@Fnumber Where  FItemID=@FItemID";
            kvp = dbCon.CreateCondParam(new Dictionary<string, object>() { { "FParentID", drData["FParentID"] }, { "FDeleted", drData["FDeleted"] }, { "Fnumber", drData["Fnumber"] }, { "FName", drData["FName"] }, { "FShortNumber", drData["FShortNumber"] }, { "FItemID", drData["FItemID"] } });
            dbCon.ExecuteSql(strSql, kvp.Value);
        }
        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FParentID", StringComparison.InvariantCultureIgnoreCase))
            {
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                if (arrFnumber.Length > 1)
                {
                    drDestData[strFieldName] = GetFParentID(dbOrgCon, dbDestCon, arrFnumber);
                }
                else
                {
                    drDestData[strFieldName] = "0";
                };
            }
            else if (strFieldName.Equals("FItemID", StringComparison.InvariantCultureIgnoreCase))
            {
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                string strFParentID= CvtFunc.ToString(drDestData["FParentID"]);
                if(strFParentID.Length==0)
                {
                    DAL_GetFieldData(  dbOrgCon,   dbDestCon,   "FParentID",   drOrgData,   drDestData);
                    strFParentID= CvtFunc.ToString(drDestData["FParentID"]);
                }
                drDestData[strFieldName] = GetFItemID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FParentID"]), strFParentID, arrFnumber);
                
            }

            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetFParentID(DBAccess dbOrgCon, DBAccess dbDestCon, string[] arrFnumber)
        {
            const string strFItemClassID = "2";
            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();
            for (int i = 0; i < arrFnumber.Length-1; i++)
            {
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFItemId;
                }
                //得到每级的FItemId
                strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, i + 1,string.Empty, strFParentID, string.Join(".", arrFnumber, 0, i + 1), intFDetail);

                UpdateAccess_t_Department(dbDestCon, strFItemId, strFParentID);
            }
            _strFDepartmentID = strFItemId;
            return strFItemId;
        }

        private  void UpdateAccess_t_Department(DBAccess dbDestCon, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from Access_t_Department where FItemID=@FItemID)
                begin
                 Insert into Access_t_Department(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FItemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
                end
                ";
            dbDestCon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FItemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }
        private string GetFItemID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFParentID, string strFParentID, string[] arrFnumber)
        {
            const string strFItemClassID = "2";
            const int intFDetail = 1;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();

            

            strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, arrFnumber.Length, strOrgFParentID, strFParentID, string.Join(".", arrFnumber, 0, arrFnumber.Length), intFDetail);
            
            UpdateAccess_t_Department(dbDestCon, strFItemId, strFParentID);

            return strFItemId;
        }

        string _strFDepartmentID = null;
        internal string FDepartmentID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFDepartmentID)
        {
            if (strOrgFDepartmentID.Equals("0") || strOrgFDepartmentID.Length == 0)
            {
                return "";
            }
             
            

            return GetDestMapKeyValue(dbOrgCon,  dbDestCon,  strOrgFDepartmentID);
        }
    }
}